<?php
namespace Home\Controller;
use  Think\Controller;

class ShowController extends Controller{
    
    // 展示因为名额不足而升级失败的用户
    public function index(){

        $res_status = M('status')->where('status = 1')->select();
        if($res_status){

            foreach($res_status as $k=>$v){
                check_user_status();
            }
        }

        $res_show = M('status')->where('status = 2 and state = 1')->order('endTime desc')->select();

        foreach($res_show as $k=>$v){

            $show_userId[] = $v['userId'];
        }

        $show_userId  = array_unique($show_userId);

        $ids = implode(',',$show_userId);
        $user_res = M('user')->where('id in ('.$ids.')')->select();

        foreach($user_res as $k=>$v){

            if($v['level'] == 2){

                if($v['picId']){
                    $pic_id2[] = $v['picId'];
                }

                if($v['detailId']){
                    $detail_id2[] = $v['detailId'];
                }
            }
            if($v['level'] == 3){

                if($v['picId']){
                    $pic_id3[] = $v['picId'];
                }

                if($v['detailId']){
                    $detail_id3[] = $v['detailId'];
                }

            }

        }

        $pic_id2 = implode(',',$pic_id2);
        $pic_id3 = implode(',',$pic_id3);
        $detail_id2 = implode(',',$detail_id2);
        $detail_id23 = implode(',',$detail_id3);

        $user2_pic = M('pic2')->where("id in (".$pic_id2.")")->select();
        $user3_pic = M('pic3')->where("id in (".$pic_id3.")")->select();
        $user2_detail = M('detail2')->where("id in (".$detail_id2.")")->select();
        $user3_detail = M('detail3')->where("id in (".$detail_id3.")")->select();

        foreach($user_res as $k=>$v){

            if($v['levle'] == 2){

                foreach($user2_pic as $k2=>$v2){

                    if($v['picId'] == $v2['id']){

                        $user_res[$k]['pic'] = $v2['pic'];
                    }
                }

                foreach($user2_detail as $k3=>$v3){

                    if($v['detailId'] == $v3['id']){

                        $user_res[$k];
                    }
                }
            }


            if($v['levle'] == 3){

                foreach($user3_pic as $k4=>$v4){

                    if($v['picId'] == $v4['id']){

                        $user_res[$k]['pic'] = $v4['pic'];
                    }
                }

                foreach($user3_detail as $k5=>$v5){

                    if($v['detailId'] == $v5['id']){

                        $user_res[$k];
                    }
                }
            }
        }

        $data = genBody('1',$res_show);
        $this->ajaxReturn($data,'JSON');

//        $this->assign('data',$res_show);
//        $this->display();
    }

    // 发出邀请信息
    public function invite(){
        
        $userId = I('userId');
        $level  = I('level');

        $id = $_SESSION['user']['userId'];
        $res_user = M('user')->where("id = '".$id."'")->find();
        
        if($level == 2){
           
           if($res_user['level'] == 3 || $res_user['level'] == 4){

                $int = 1;
           }else{

                $int = 2;
           }
        }else if($level == 3){

           if($res_user['level'] == 4){

                $int = 1;
           }else{

                $int = 2;
           }
        }


        if($int == 2){

            $data = genBody('0','您的等级不足，不能邀请该用户');
            $this->ajaxReturn($data,'JSON');
        }

        $arr_invite['code'] =  $res_user['code'];
        $arr_invite['pid'] = $res_user['id'];
        $arr_invite['time'] = time();
        $arr_invite['object'] = $userId;

        $dbtable = 'invite'.$res_user['level'];
        $dbtable2 = 'userstatus'.$res_user['level'];

        $res_userstatus = M($dbtable2)->where("userId = '".$res_user['id']."'")->find();

        if($res_userstatus['status'] != 1){

            $data = genBody('1','您的名额已经用完，不能再次邀请');
        }

        $res_invite = M($dbtable)->add($arr_invite);// 发出邀请

    }
}